$(function () { 
    $('.submenu').show();

    var page = 1;
    var pageSize = 5;

    function render() { 
        $.ajax({
            url:'/category/querySecondCategoryPaging',
            type:'get',
            data:{
                page:page,
                pageSize:pageSize
            },
            success:function (data) { 
                console.log(data);
                var html=template('subcategory',data);
                $('tbody').html(html);

                $(".pagination").bootstrapPaginator({
                    bootstrapMajorVersion: 3,
                    currentPage : page,
                    totalPages:Math.ceil(data.total / pageSize),
                    onPageClicked: function(a,b,c,index) {
                        page=index;
                        render();
                    }
                });
            }
        })
     }
     render();
     $('.addcategory').click(function () { 
        $('#addcategory-modal').modal('show');

      })

      $.ajax({
          url:'/category/queryTopCategoryPaging',
          type:'get',
          data:{page:1,pageSize:100},
          success:function (data) { 
            console.log(data);
            var html = template('cate-dropdown',data);
            $('.dropdown-menu').html(html);
           }
      })

      $('.dropdown-menu').on('click','a',function () { 
        $('.dropdown-toggle .text').html($(this).html());
        $('.dropdown input').val($(this).data('id'));
        var validator = $("form").data('bootstrapValidator'); 
        validator.updateStatus("categoryId", "VALID");

       })
       
       $('#fileupload').fileupload({
           dataType:'json',
           done:function (e,data) { 
            //    console.log(data.result);
               $('.form-group img').attr('src',data.result.picAddr);
               $("input[name=brandLogo]").val(data.result.picAddr);
               var validator = $("form").data('bootstrapValidator'); 
               validator.updateStatus("brandLogo", "VALID");
            }
       })
       var $form = $("form");

       $('button[type=submit]').click(function () { 
            $form.submit();
        })


      
       //设置验证规则
       $form.bootstrapValidator({
        excluded: [],
           //1. 设置验证状态对应的小图标
           feedbackIcons: {
               //如果验证通过，显示的小图标
               valid: 'glyphicon glyphicon-ok',
               //验证不通过
               invalid: 'glyphicon glyphicon-remove',
               //正在验证的状态
               validating: 'glyphicon glyphicon-refresh'
           },
   
           fields: {
            brandName: {
                   validators: {
                       //1. 规则,验证是否输入了字符
                       notEmpty: {
                           message: "请输入二级分类的名称"
                       }
                   }
               },
   
               categoryId: {
                   validators: {
                       //1. 规则,验证是否输入了字符
                       notEmpty: {
                           message: "请选择一级分类"
                       }
                   }
               },

               brandLogo: {
                 validators: {
                    //1. 规则,验证是否输入了字符
                    notEmpty: {
                        message: "请上传图片"
                    }
                }
            }
           }
       });
   
       //表单验证成功之后的事件处理
       $form.on("success.form.bv", function(target) {
           //阻止默认事件（提交表单）
           target.preventDefault();
           // alert("哈哈， 表单验证已成功");
           // console.log($form.serialize());
   
           $.ajax({
               url: "/category/addSecondCategory",
               type: "POST",
               data: $form.serialize(),
               success: function(data) {
                   console.log(data);
                   if(data.success){
                       
                    $('#addcategory-modal').modal('hide');
                    render();
                   }
               }
           });
       });
 })